package com.ruoyi.vote.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.vote.admin.domain.VoteSign;
import com.ruoyi.vote.admin.service.IWxUserSubService;
import com.ruoyi.vote.common.constants.VoteConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.vote.admin.domain.bo.WxUserSubBo;
import com.ruoyi.vote.admin.domain.vo.WxUserSubVo;
import com.ruoyi.vote.admin.domain.WxUserSub;
import com.ruoyi.vote.admin.mapper.WxUserSubMapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户订阅Service业务层处理
 *
 * @author 筑巢
 * @date 2023-07-25
 */
@RequiredArgsConstructor
@Service
public class WxUserSubServiceImpl extends ServiceImpl<WxUserSubMapper,WxUserSub> implements IWxUserSubService {

    private final WxUserSubMapper baseMapper;

    /**
     * 条件获取对应的订阅人
     * @return
     */
    @Override
    public List<WxUserSubVo> queryQw(QueryWrapper<WxUserSub> qw) {
        qw.eq("wus.is_send", VoteConstants.VOTE_STATUS_SUCCESS);
        qw.eq("wus.del_flag", VoteConstants.VOTE_STATUS_SUCCESS);
        qw.apply("DATE_ADD(wus.subscribed_at, INTERVAL 7 DAY) >= NOW()");
        qw.groupBy("wus.module","wus.eventid","wus.openid");
        return baseMapper.selectSubUser(qw);
    }

    /**
     * 查询用户订阅列表
     */
    @Override
    public TableDataInfo<WxUserSubVo> queryPageList(WxUserSubBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxUserSub> lqw = buildQueryWrapper(bo);
        Page<WxUserSubVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<WxUserSub> buildQueryWrapper(WxUserSubBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxUserSub> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), WxUserSub::getOpenid, bo.getOpenid());
        lqw.eq(StringUtils.isNotBlank(bo.getTempModel()), WxUserSub::getTempModel, bo.getTempModel());
        lqw.eq(StringUtils.isNotBlank(bo.getTampId()), WxUserSub::getTampId, bo.getTampId());
        lqw.eq(bo.getSubscribedAt() != null, WxUserSub::getSubscribedAt, bo.getSubscribedAt());
        lqw.eq(bo.getIsOneTime() != null, WxUserSub::getIsOneTime, bo.getIsOneTime());
        return lqw;
    }

    /**
     * 新增用户订阅
     */
    @Override
    public Boolean insertByBo(WxUserSubBo bo) {
        WxUserSub add = BeanUtil.toBean(bo, WxUserSub.class);
        add.setOpenid(LoginHelper.getLoginUser().getOpenid());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 批量删除用户订阅
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
